------------------------------------------------------------
-- EasyTrinket Buttonfacade Support
--
-- Special thanks: XBug
-- From: http://bbs.game.mop.com/viewthread.php?tid=1644785&extra=page%3D1&page=16
------------------------------------------------------------

if not IsAddOnLoaded("ButtonFacade") or not LibStub then return end
local BF = LibStub("AceAddon-3.0"):GetAddon("ButtonFacade")
local LBF = LibStub("LibButtonFacade", true)
if not BF or not LBF then return end

-- ButtonFacade is loaded
local L = EASYTRINKET_LOCALE; -- locale table

local ET = "EasyTrinket"
local BFET = BF:NewModule(ET)
local defaults	= {
	profile = {
		["*"] = {
			Skin = "Blizzard",
			Gloss = 0,
			Backdrop = false,
			Colors = {},
		},
	},
}

local db

function BFET:OnInitialize()
	db = self:RegisterNamespace(ET, defaults)
	self.db = db
end

function BFET:SkinCallback(skin, gloss, backdrop, group, button, colors)
	if not group then return end
	self.db.profile[group].Skin = skin
	self.db.profile[group].Gloss = gloss
	self.db.profile[group].Backdrop = backdrop
	self.db.profile[group].Colors = colors
end

function BFET:SetupGroup(group, prefix, count)
	local etgroup = LBF:Group(ET, group)	
	for i = 1, count do
		local button = getglobal(prefix..i)
		if button then
			etgroup:AddButton(button)
		end
	end	
	etgroup:Skin(self.db.profile[group].Skin, self.db.profile[group].Gloss, self.db.profile[group].Backdrop, self.db.profile[group].Colors)
end

function BFET:Load()
	self:SetupGroup(L["main buttons"], "EasyTrinket_Trinket", 2)
	self:SetupGroup(L["menu buttons"], "EasyTrinket_MenuTrinket", EASYTRINKET_MENUBUTTON_NUM)
	LBF:RegisterSkinCallback(ET, self.SkinCallback, self)
end

function BFET:OnEnable()
	self:Load()
end